package com.starlight.flower_server.domain.service.impl;

import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.FlowerUserRole;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRoleRepository;
import com.starlight.flower_server.domain.service.FlowerUserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:30
 */
@Service
public class FlowerUserRoleServiceImpl implements FlowerUserRoleService {
    @Resource
    private FlowerUserRoleRepository flowerUserRoleRepository;

    public Integer modifyUserRoleById(Long id,Long roleId){
        FlowerUserRole flowerUserRole=flowerUserRoleRepository.queryDetail(id);
        if (flowerUserRole.getRoleId().equals(5L)){
            throw new BadRequestException("权限更改失败：无法更改被封禁账户的权限，请先解封。", Status.EXCEPTION);
        }
        if (roleId.equals(5L)||roleId.equals(1L)){
            throw new BadRequestException(Status.PARAMETER_ERROR.getMessage(), Status.INVALID_ROLE);
        }
        flowerUserRole.setRoleId(roleId);
        return flowerUserRoleRepository.modify(flowerUserRole);
    }

    @Override
    public Integer banUserById(Long id) {
        FlowerUserRole flowerUserRole=flowerUserRoleRepository.queryDetail(id);
        flowerUserRole.setRoleId(5L);
        return flowerUserRoleRepository.modify(flowerUserRole);
    }

    @Override
    public Integer recoverUserById(Long id) {
        FlowerUserRole flowerUserRole=flowerUserRoleRepository.queryDetail(id);
        if (flowerUserRole.getRoleId().equals(5L)){
            flowerUserRole.setRoleId(2L);
        }else {
            throw new BadRequestException(Status.PARAMETER_ERROR.getMessage(), Status.INVALID_ROLE);
        }
        return flowerUserRoleRepository.modify(flowerUserRole);
    }


}
